//
//  ViewController.m
//  TCP
//
//  Created by LiTengFei on 15/5/21.
//  Copyright (c) 2015年 HangZhou HugeWing. All rights reserved.
//

#import "ViewController.h"
#import "AsyncSocket.h"
@interface ViewController () <AsyncSocketDelegate>
@property (nonatomic,strong) AsyncSocket * scoket;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.scoket = [[AsyncSocket alloc]initWithDelegate:self];

    NSError *err = nil;

    if(![self.scoket connectToHost:@"127.0.0.1" onPort:1999 error:&err]){
        NSLog(@"%@",err);
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - onScoket

-(void)onSocket:(AsyncSocket *)sock didAcceptNewSocket:(AsyncSocket *)newSocket{

}

-(void)onSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag{
    NSLog(@"%ld",tag);
    [self.scoket readDataWithTimeout:1 tag:1];
}

-(void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port{

    NSLog(@"%@",[self.scoket connectedHost]);

    NSLog(@"onSocket:%p didConnectToHost:%@ port:%hu", sock, host, port);

    NSString * str = @"api/login/login?tel=13208038156&&password=123456";

    NSData * data = [str dataUsingEncoding:NSUTF8StringEncoding];

    [self.scoket writeData:data withTimeout:-1 tag:1];
}
-(void)onSocket:(AsyncSocket *)sock didReadPartialDataOfLength:(NSUInteger)partialLength tag:(long)tag
{

}
-(void) onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
    NSLog(@"%@",data);
    NSString* aStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"===%@",aStr);

}
- (void)onSocket:(AsyncSocket *)sock didSecure:(BOOL)flag
{
    NSLog(@"onSocket:%p didSecure:YES", sock);
}
- (void)onSocket:(AsyncSocket *)sock willDisconnectWithError:(NSError *)err
{
    NSLog(@"onSocket:%p willDisconnectWithError:%@", sock, err);
}

- (void)onSocketDidDisconnect:(AsyncSocket *)sock
{
        //断开连接了
    NSLog(@"onSocketDidDisconnect:%p", sock);
}

- (void)viewDidUnload {
    self.scoket=nil;
}

@end













